<?php
/**
 * 出差申请人导入
 * 对于特殊的领导人，出差新建一个虚拟部门，部门id 9999 审批人为Mike
 */
apf_require_class('Oa_Job_Attendance_Abstract');
class Oa_Job_Attendance_TripManager extends Oa_Job_Attendance_Abstract {
    /** 考勤系统读 **/
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";

    public function run(){
        $this->log("准备插入数据...");
        $this->init_data();
        $this->log("执行完毕...");
    }

    /**
     * 写入数据库操作
     */
    public function init_data(){
        $data = $this->regular();
        $sql = "replace into trip_manager(user_id,department_id,user_email) values(?,?,?)";
        foreach ($data as $v){
            $result = $this->execute($sql,array($v['user_id'],$v['department_id'],$v['user_email']),$this->writedb);
            echo $result.PHP_EOL;
        }
    }

    /**
     * 规则
     * @return array('user_id','department_id','user_email')
     */
    public function regular(){
        $date = array();//部门对应的信息
        $department_info = $this->get_department_info();
        print_r($department_info[501]);
        foreach ( $department_info as $department_id=>$info ) {
            $form = substr ( $info['department_name'], 0, 6 );
            $back = substr ( $info['department_name'], - 9, 9 );
            /*
             * if($form == '二手'){ $to2 = 'michaellv@anjukeinc.com'; }
             * elseif($form == '新房' && ($back == '运营部' || $back == '媒体部')){ $to2
             * = 'joychen@anjukeinc.com'; } elseif($form == '新房' && ($back !=
             * '运营部' && $back != '媒体部')){ $to2 = 'michaellv@anjukeinc.com'; }
             * else{ $to2 = 'mikeliang@anjukeinc.com'; }
             */
            /**
            $smail = array (
                    'michaellv@anjukeinc.com',
                    'garyhu@anjukeinc.com',
                    'julianwang@anjukeinc.com',
                    'justin@anjukeinc.com',
                    'joychen@anjukeinc.com',
                    'echoyang@anjukeinc.com',
                    'vincentchang@anjukeinc.com',
                    'henrytang@anjukeinc.com'
            );
            */
            if ($back == '产品部' && ($form != '移动' || $form != '商业')) {//**还差个特殊处理** 产品部、吕广渝、胡建东、julian、justin、joy、echo、常瑶琦、唐皓——Mike
                $to2 = 'mikeliang@anjukeinc.com';
            } elseif ($back == '销售部' || $back == '客服部') { //1.全国销售部&客服部——吕广渝
                $to2 = 'michaellv@anjukeinc.com';
            } elseif ($back == '数据部' || ($form == '商业' && ($back == '运营部' || $back == '产品部'))) {//2.商业地产事业部&BI部——胡建东
                $to2 = 'garyhu@anjukeinc.com';
            } elseif ($back == '财务部' || ($form == '租房' && ($back == '运营部' || $back == '产品部'))) {//3.租房事业部&全国财务部——Julian
                $to2 = 'julianwang@anjukeinc.com';
            } elseif (($form == '移动' && $back != '市场部') || $back == '技术部') {//4.移动事业部&所有技术部——Justin
                $to2 = 'justin@anjukeinc.com';
            } elseif ($form == '新房' && ($back == '运营部' || $back == '媒体部')) {//5.全国媒体部——Joy //9.新房运营部——Joy
                $to2 = 'joychen@anjukeinc.com';
            } elseif ($form == '人力') { //6.全国HR——Echo
                $to2 = 'echoyang@anjukeinc.com';
            } elseif ($form == '新房' && $back == '营中心') { // 新房销售营销中心 7.新房销售运营中心——常瑶琦
                $to2 = 'vincentchang@anjukeinc.com';
            } elseif ($form == '二手' && ($back == '营中心' || $back == '运营部')) {//8.二手房销售运营中心——吕广渝//9.二手房运营部——吕广渝
                $to2 = 'michaellv@anjukeinc.com';
            } elseif ($back == '市场部') { //10.全国市场部——Henry(唐皓）
                $to2 = 'henrytang@anjukeinc.com';
            } elseif ($back == '办公室') {//总裁办公室
                //$to2 = 'mikeliang@anjukeinc.com';
            }

            $user_info = $this->get_user_info_by_email($to2);
            $user_id = $user_info['user_id'];
            $date[] = array('user_id'=>$user_id,'department_id'=>$department_id,'user_email'=>$to2);
        }

        /*
         *特殊人员贵boss管理，规定特定部门id未 9999
         */
        //$mike = $this->get_user_info_by_email('mikeliang@anjukeinc.com');
        //$mike_id = $mike['user_id'];
        //$date[] = array('user_id'=>$mike_id,'department_id'=>'9999','user_email'=>'mikeliang@anjukeinc.com');
        return $date;
    }

    /**
     * 所有现存在的部门信息 is_del = 0
     */
    public function get_department_info(){
        $date = array();
        $sql = "SELECT * from department WHERE is_del = 0";
        $rows = $this->get_db_rows($sql, $this->readdb);
        foreach ($rows as $row){
            $date[$row['department_id']] = $row;
        }
        return $date;
    }
    /**
     * 根据邮件地址获取用户信息
     */
    public function get_user_info_by_email($email){
        $sql = "SELECT * FROM user_info WHERE email = '{$email}' limit 1";
        $row = $this->get_db_row($sql, $this->readdb);
        return $row;
    }

}